/**
 * 
 */
package com.orcwar.gui.views;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orcwar.R;
import com.orcwar.engine.IOWGameListener;
import com.orcwar.engine.OWMap;
import com.orcwar.engine.OWPlayer;
import com.orcwar.engine.OWTile;
import com.orcwar.engine.OWUnit;
import com.orcwar.engine.randomizer.OWOrcNameRandomizer;

/**
 * @author Callil
 *
 */
public class OWJournalView extends LinearLayout implements IOWGameListener {
	private List<String> journal = new ArrayList<String>(5);
	
	private List<TextView> textViews = new ArrayList<TextView>(5);

	private Activity activity;
	/**
	 * @param context
	 */
	public OWJournalView(Activity activity) {
		super(activity);
		this.activity = activity;
		View view = inflate(getContext(), R.layout.journal, null);
        addView(view);
        textViews.add((TextView) findViewById(R.id.journalLine1));
        textViews.add((TextView) findViewById(R.id.journalLine2));
        textViews.add((TextView) findViewById(R.id.journalLine3));
        textViews.add((TextView) findViewById(R.id.journalLine4));
        textViews.add((TextView) findViewById(R.id.journalLine5));
        updateJournal();
	}
	
	/**
	 * Update the textview that represent the journal.
	 * @param texts A list of string. The 
	 */
	public void updateJournal() {
		if (journal == null || journal.size() == 0) {
			flush();
			return;
		}
		
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				for (int i=0; i<5 ; i++) {
					updateOneLine(i);
				}
			}
		});
	}
	
	/**
	 * Flush the text views.
	 */
	private void flush() {
		for (TextView textView : textViews) {
			textView.setText("");
		}
	}
	
	/**
	 * Update one of the text views.
	 * @param lineNumber the line
	 */
	private void updateOneLine(int lineNumber) {
		if (journal.size() > (4 - lineNumber)) {
			textViews.get(lineNumber).setText(journal.get(journal.size() - (5 - lineNumber)));
		} else {
			textViews.get(lineNumber).setText("");
		}
	}
	
	/**
	 * Add an entry to the journal.
	 * @param entry The entry.
	 */
	private void addEntry(String entry) {
		if (journal.size() == 5) {
			journal.remove(0);
		}
		journal.add(entry);
		updateJournal();
	}

	@Override
	public void onLevelStart(OWMap map) {
		addEntry("Level starts.");
	}

	@Override
	public void onLevelEnd(OWMap map, OWPlayer winner) {
		addEntry("Level Ends.");
	}

	@Override
	public Runnable onTurnStart(OWPlayer currentPlayer, int turn) {
		addEntry("Turn " + turn + " starts.");
		return null;
	}

	@Override
	public void onTurnEnd(OWPlayer currentPlayer) {
		addEntry("Turn ends.");
	}

	@Override
	public void onUnitMove(OWUnit unit, OWTile startTile, OWTile endTile,
			List<OWTile> path) {
		addEntry(OWOrcNameRandomizer.generateName() + " moves on tile " + endTile.getIdStr() + ".");
	}

	@Override
	public void onUnitAttack(OWUnit unit, OWTile attackedTile, boolean success, 
			List<OWUnit> attackedUnits) {
		if (attackedUnits != null && attackedUnits.size() > 0) {
			for (OWUnit attackedUnit : attackedUnits) {
				addEntry(OWOrcNameRandomizer.generateName() + " attacked " + attackedUnit.getClass().getSimpleName()
						+ " on tile " + attackedTile.getId() + ".");
			}
		} else {
			addEntry(OWOrcNameRandomizer.generateName() + " attacked the tile " + attackedTile.getId() + ".");
		}
	}

	@Override
	public void onUnitDeath(OWUnit unit) {
		addEntry("FUCK MY EVERYDAY !");
		//addEntry(unit.getClass().getSimpleName() + " dies !");

	}

	@Override
	public void onUnitTurnEnd(OWUnit unit) {
		addEntry(unit.getClass().getSimpleName() + "'s turn ends.");
	}

}
